//
//  SpecialStoreViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/17.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "SpecialStoreViewController.h"

#import "KK_TabBarView.h"
#import "GoodsCollectionViewCell.h"
#import <SwipeView/SwipeView.h>
#import "GoodsCatalogModel.h"
#import "BannerCell.h"
#import "ProductDetailsViewController.h"

#define TabBar_Tag(index)      1000+index //目录

@interface SpecialStoreViewController ()<KK_TabBarViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UISearchBarDelegate>{
    NSInteger categoryIndex;
    NSInteger orderByIndex;
    NSString *categoryId;
}
@property (nonatomic , weak) IBOutlet UISearchBar *searchBar;
@property (nonatomic , weak) IBOutlet UIView *tabBarBackground;
@property (nonatomic , weak) IBOutlet UICollectionView *goodsCollectionView;

@property (nonatomic , strong) NSMutableArray *tabBarViews;//分类选项
@property (nonatomic , strong) NSMutableArray *goodsList;

@property (nonatomic,strong) NSArray *categoryArray;


@property (weak, nonatomic) IBOutlet UICollectionView *cCollectionView;

@property (weak, nonatomic) IBOutlet UIButton *priceButton;
@property (weak, nonatomic) IBOutlet UIButton *sellButton;




@end

@implementation SpecialStoreViewController




-(void)fetchCategoryList{
    [[GoodsCatalogModel sharedModel] fetchgoodsSecondCatalogListWithCatalogID:@"09610CBE73DCA61BE05019AC911E17CF" result:^(NSArray *array, NSString *msg) {
        self.categoryArray = [NSArray arrayWithArray:array];
        [self initCategoryView];
    }];
}

- (IBAction)orderByAction:(UIButton *)sender {
    
    if ((sender == _priceButton && _priceButton.selected) || (sender == _sellButton && _sellButton.selected)) {
        return;
    }
    
    if (sender != _priceButton && _priceButton.selected) {
        _priceButton.selected = NO;
        sender.selected = YES;
    }else if(sender != _sellButton && _sellButton.selected){
        _sellButton.selected = NO;
        sender.selected = YES;
    }
    
    NSLog(@"%d",sender.tag);
}

-(void)fetchListData{
    [_goodsList removeAllObjects];
    [[GoodsCatalogModel sharedModel] fetchGoodsListWithFirstCatalogID:@"09610CBE73DCA61BE05019AC911E17CF" secondCatalogID:categoryId key:@"" sortFlag:orderByIndex+1 currentPage:1 pageSize:1000 result:^(NSArray *array, NSString *msg) {
        [_goodsList addObjectsFromArray:array];
        [self.goodsCollectionView reloadData];
    }];
}


-(void)initCategoryView{
    [self.cCollectionView reloadData];
    [self fetchListData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _goodsList = [NSMutableArray array];
    categoryIndex = 0;
    orderByIndex = 0;
    [self fetchCategoryList];
    
//    [self insertTabBarView:nil index:0];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - action
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)sectio{
    if (collectionView.tag == 100) {
        return _categoryArray.count;
    }
    return [_goodsList count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView.tag == 100) {
        BannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BannerCell" forIndexPath:indexPath];
        
        GoodsFirstCatalogItem *item = _categoryArray[indexPath.row];
        cell.titleLabel.text = item.CATALOG_NAME;
        if (indexPath.row == categoryIndex) {
            [cell selected:YES];
        }
        else
            [cell selected:NO];

        return cell;
    }
    else
    {
        GoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCollectionViewCell" forIndexPath:indexPath];

            GoodsInfoItem *item = _goodsList[indexPath.row];
            cell.nameLabel.text = item.PRODUCT_NAME;
        cell.goodsImageview.layer.masksToBounds = YES;
        cell.goodsImageview.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cell.goodsImageview.layer.borderWidth = 1.f;
            [cell.goodsImageview sd_setImageWithURL:[NSURL URLWithString:item.PRODUCT_IMG_URL] placeholderImage:nil];
            cell.priceLabel.text = [NSString stringWithFormat:@"%@",_RMB(item.PRE_PRICE)];
            cell.marketLabel.text = [NSString stringWithFormat:@"销量:%d",[item.SALE_NUM intValue]];

        return cell;
    }
    
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag == 100) {
        if (categoryIndex != indexPath.row) {
            GoodsFirstCatalogItem *item = _categoryArray[indexPath.row];
            categoryId = item.CATALOG_ID;
            categoryIndex = indexPath.row;
            [self.cCollectionView reloadData];
        }
        
    }
}


- (IBAction)goToStoreAction:(id)sender {
    [self.navigationController.tabBarController setSelectedIndex:1];
}


 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     NSIndexPath *indexPath = [_goodsCollectionView indexPathForCell:sender];
     
     GoodsInfoItem *item = _goodsList[indexPath.row];
     [[segue destinationViewController] setProductid:item.PRODUCT_ID];

 }
@end
